using Boo.Lang.Compiler.Ast;

namespace Mite.DSL
{
	public class VersionStandardsClassTransformer : DepthFirstTransformer
	{
		public override void OnClassDefinition(ClassDefinition node)
		{
			if (!node.Name.EndsWith("Module"))
			{
				bool containsVersion =
					node.Members.ToArray().Contains(
						member => member.NodeType == NodeType.Method && member.Name.Equals("Version"));
				if (!containsVersion)
				{
					var versionReturn = new ReturnStatement(node.LexicalInfo);
					versionReturn.Expression = new IntegerLiteralExpression(node.LexicalInfo, node.Name.GetVersion());

					var versionGetter = new Method(node.LexicalInfo);
					versionGetter.Name = "getter_Version";
					versionGetter.Body = new Block(node.LexicalInfo);
					versionGetter.Body.Add(versionReturn);

					var version = new Property(node.LexicalInfo);
					version.Name = "Version";
					version.Modifiers = TypeMemberModifiers.Public | TypeMemberModifiers.Override;
					version.Getter = versionGetter;

					node.Members.Add(version);
				}
			}
			base.OnClassDefinition(node);
		}
	}
}